home *** CD-ROM | disk | FTP | other *** search
- #include "kant search.h"
- #include "text twiddling.h"
- #include "dialogs.h"
- #include "util.h"
- #include "prefs.h"
- #include "main.h"
- #include "window layer.h"
- #include "program globals.h"
-
- extern Boolean gCustomCursor; /* see environment.c */
-
- #define findDialog 500
-
- Boolean gStartFromTop;
- Boolean gIgnoreCase;
- Str255 gFindString;
- Str255 gReplaceString;
- short gLastFindPosition;
-
- static Boolean SearchDoneQQ(short strPos, short strLen);
-
- void SetLastFindPosition(short val)
- {
- gLastFindPosition=val;
- }
-
- short SearchForwards(TEHandle hTE, short *foundLength)
- {
- short offset;
- short max;
- short strPos;
- short strLen;
- unsigned char *a;
- unsigned char theChar, matchChar;
-
- max=(**hTE).teLength;
- HLock((**hTE).hText);
- a=(unsigned char*)(*((**hTE).hText));
- strPos=1;
- strLen=gFindString[0];
-
- offset=gLastFindPosition;
- while ((offset<max) && (!SearchDoneQQ(strPos, strLen)))
- {
- if (gIgnoreCase)
- {
- theChar=a[offset];
- if ((theChar>='A') && (theChar<='Z'))
- theChar|=0x20;
- matchChar=gFindString[strPos];
- if ((matchChar>='A') && (matchChar<='Z'))
- matchChar|=0x20;
- if (theChar==matchChar)
- strPos++;
- else
- strPos=1;
- }
- else
- {
- if (a[offset]==gFindString[strPos])
- strPos++;
- else
- strPos=1;
- }
- offset++;
- }
-
- HUnlock((**hTE).hText);
- if ((strLen>0) && (strPos>strLen))
- {
- SetLastFindPosition(offset+1);
- *foundLength=strLen;
- return offset-strLen;
- }
-
- return -1;
- }
-
- Boolean SearchDoneQQ(short strPos, short strLen)
- {
- Boolean done;
-
- done=FALSE;
- if (gFindString[0]!=0x00)
- done=(strPos>strLen);
-
- return done;
- }
-
- Boolean DoFindDialog(void)
- {
- DialogPtr theDlog;
- short itemSelected;
- Handle itemH;
- short itemType;
- Rect box;
- ModalFilterUPP procFilter = NewModalFilterProc(TwoButtonFilter);
- UserItemUPP userUPP=NewUserItemProc(OutlineDefaultButton);
- Boolean ignoreCase, startFromTop;
- Boolean found;
-
- SetCursor(&qd.arrow);
- gCustomCursor=FALSE;
-
- PositionDialog('DLOG', findDialog);
- theDlog = GetNewDialog(findDialog, 0L, (WindowPtr)-1L);
-
- GetDItem(theDlog, 3, &itemType, &itemH, &box);
- InsetRect(&box, -4, -4);
- SetDItem(theDlog, 3, itemType, (Handle)userUPP, &box);
-
- ignoreCase=gIgnoreCase;
- startFromTop=gStartFromTop;
-
- GetDItem(theDlog, 7, &itemType, &itemH, &box);
- SetIText(itemH, gFindString);
- SelIText(theDlog, 7, 0, 32767);
- GetDItem(theDlog, 8, &itemType, &itemH, &box);
- SetIText(itemH, gReplaceString);
- GetDItem(theDlog, 9, &itemType, &itemH, &box);
- SetControlValue((ControlHandle)itemH, ignoreCase ? 1 : 0);
- GetDItem(theDlog, 10, &itemType, &itemH, &box);
- SetControlValue((ControlHandle)itemH, startFromTop ? 1 : 0);
-
- SetWTitle((WindowPtr)theDlog, "\pFind");
- ShowWindow(theDlog);
- itemSelected=0;
- while ((itemSelected!=1) && (itemSelected!=2) && (itemSelected!=4))
- {
- ModalDialog(procFilter, &itemSelected);
-
- switch (itemSelected)
- {
- case 9: /* ignore case checkbox */
- ignoreCase=!ignoreCase;
- GetDItem(theDlog, 9, &itemType, &itemH, &box);
- SetControlValue((ControlHandle)itemH, ignoreCase ? 1 : 0);
- break;
- case 10: /* start from top checkbox */
- startFromTop=!startFromTop;
- GetDItem(theDlog, 10, &itemType, &itemH, &box);
- SetControlValue((ControlHandle)itemH, startFromTop ? 1 : 0);
- break;
- }
- }
-
- if (itemSelected!=2)
- {
- GetDItem(theDlog, 7, &itemType, &itemH, &box);
- GetIText(itemH, gFindString);
- GetDItem(theDlog, 8, &itemType, &itemH, &box);
- GetIText(itemH, gReplaceString);
- GetDItem(theDlog, 9, &itemType, &itemH, &box);
- gIgnoreCase=(GetControlValue((ControlHandle)itemH)==0x00) ? FALSE : TRUE;
- GetDItem(theDlog, 10, &itemType, &itemH, &box);
- gStartFromTop=(GetControlValue((ControlHandle)itemH)==0x00) ? FALSE : TRUE;
- }
-
- HideWindow(theDlog);
- DisposeDialog(theDlog);
- DisposeRoutineDescriptor(procFilter);
- DisposeRoutineDescriptor(userUPP);
-
- while (HandleSingleEvent(FALSE)) {};
-
- if (itemSelected!=2)
- {
- SaveThePrefs();
- if (gStartFromTop)
- SetWindowLastFindPosition(GetFrontDocumentWindow(), 0);
- if (itemSelected==1)
- {
- found=DoFindAgain();
- if (!found)
- SysBeep(7);
- return found;
- }
- else
- {
- DoReplaceAll();
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- Boolean DoFindAgain(void)
- {
- TEHandle hTE;
- short newOffset;
- short len;
- WindowPtr theWindow;
-
- if ((theWindow=GetFrontDocumentWindow())==0L)
- return FALSE;
-
- if ((hTE=GetWindowTE(theWindow))==0L)
- return FALSE;
-
- SetLastFindPosition(GetWindowLastFindPosition(theWindow));
- newOffset=SearchForwards(hTE, &len);
- if (newOffset!=-1)
- {
- TESetSelect(newOffset, newOffset+len, hTE);
- TESelView(hTE);
- AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
- SetWindowLastFindPosition(theWindow, newOffset+len);
- return TRUE;
- }
-
- return FALSE;
- }
-
- void DoReplace(void)
- {
- WindowPtr theWindow;
- TEHandle hTE;
-
- if ((theWindow=GetFrontDocumentWindow())==0L)
- return;
- if ((hTE=GetWindowTE(theWindow))==0L)
- return;
-
- TEDelete(hTE);
- InsertBeforeStart(theWindow, gReplaceString);
- AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
- SetWindowLastFindPosition(theWindow, (**hTE).selEnd);
- }
-
- void DoReplaceAll(void)
- {
- SetWindowLastFindPosition(GetFrontDocumentWindow(), 0);
- while (DoFindAgain())
- DoReplace();
- SysBeep(7);
- // maybe show alert here saying how many were found?
- }
-
- void DoEnterString(Boolean isFindString)
- {
- WindowPtr theWindow;
-
- if ((theWindow=GetFrontDocumentWindow())==0L)
- return;
- GetSelectionString(theWindow, isFindString ? gFindString : gReplaceString);
- }
-
- Boolean AnythingToFindQQ(void)
- {
- return (gFindString[0]!=0x00);
- }
-
- Boolean AnythingToReplaceQQ(void)
- {
- return (gReplaceString[0]!=0x00);
- }
-